package org.example.service.impl;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.example.config.AlipayConfig;
import org.example.pojo.AlipayBean;
import org.example.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlipayServiceImp implements AlipayService {

    @Autowired
    AlipayConfig alipayConfig;
    @Override
    public String alipay(AlipayBean alipayBean){
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
                alipayConfig.getApp_id(),
                alipayConfig.getMerchant_private_key(),
                alipayConfig.getFormate(),
                alipayConfig.getCharset(),
                alipayConfig.getAlipay_public_key(),
                alipayConfig.getSign_type());
        //System.out.println(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl(alipayConfig.getReturn_url());
        request.setNotifyUrl(alipayConfig.getNotify_url());
        request.setBizContent("{\"out_trade_no\":\"" + alipayBean.getOut_trade_no() + "\","
                + "\"total_amount\":\"" + alipayBean.getTotal_amount() + "\","
                + "\"subject\":\"" + alipayBean.getSubject() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            // 调用SDK生成表单
            form = alipayClient.pageExecute(request).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }
}
